package com.example.myapp.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapp.AboutAppActivity;
import com.example.myapp.LoginActivity;
import com.example.myapp.R;
import com.example.myapp.UpdatePwdActivity;
import com.example.myapp.entity.UserInfo;

public class MineFragment extends Fragment {
    private View rootView;
    private TextView tv_username;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_mine,container,false);

        //初始化控件
        tv_username = rootView.findViewById(R.id.tv_username);
        
        //退出登录
        rootView.findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                new AlertDialog.Builder(getContext())
                        .setTitle("退出登录")
                        .setMessage("确定要退出登录吗？")
                        .setNegativeButton(getColoredSpannable("取消", Color.RED), null) // 取消按钮红色
                        .setPositiveButton(getColoredSpannable("确认", Color.BLUE), (dialog, which) -> {
                            // 退出登录逻辑
                            getActivity().finish();
                            startActivity(new Intent(getActivity(), LoginActivity.class));
                        })
                        .show();
            }
        });

        //修改密码
        rootView.findViewById(R.id.updatePwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
                startActivityForResult(intent,1000);
            }
        });

        //关于APP
        rootView.findViewById(R.id.aboutApp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AboutAppActivity.class));
//                startActivityForResult(intent,1000);
            }
        });

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //设置用户数据
        UserInfo userInfo = UserInfo.getUserInfo();
        if (null!=userInfo){
            tv_username.setText(userInfo.getUsername());
        }
    }


//    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(resultCode==1000){
//            getActivity().finish();
//            startActivity(new Intent(getActivity(), LoginActivity.class));
//        }
//    }

    private SpannableString getColoredSpannable(String text, int color) {
        SpannableString spannable = new SpannableString(text);
        spannable.setSpan(new ForegroundColorSpan(color), 0, text.length(), 0);
        return spannable;
    }
}